package org.gruposp2p.openid

import org.codehaus.groovy.grails.commons.ConfigurationHolder as CH

class UserController {

	
	def beforeInterceptor = [action:this.&debug]

  	def debug(){
		log.debug "##########################################################################"
		log.debug "request.getRemoteAddr(): " + request.getRemoteAddr()
		log.debug "request.getHeader(X-Forwarded-For): " + request.getHeader("X-Forwarded-For")
		log.debug "request.getHeader(Client-IP): " + request.getHeader("Client-IP")
		log.debug "request.forwardURI: " + request.forwardURI
		log.debug "params: " + params
		log.debug "##########################################################################"
  	}
	
	def index = {
		session.userClaimedId = CH.config.grails.serverURL + request.forwardURI
		log.debug("-- session.userClaimedId --" + session.userClaimedId);
		sendDiscoveryResponse()
	}
	
	private void sendDiscoveryResponse() {
		render (contentType:"application/xrds+xml") {
			'xrds:XRDS'('xmlns:xrds': 'xri://$xrds', 
						'xmlns:openid':'http://openid.net/xmlns/1.0',
						'xmlns':'xri://$xrd*($v*2.0)') {
				XRD {
					Service(priority:'0') {
						Type('http://openid.net/signon/1.0')
						Type('http://openid.net/signon/1.1')
						Type('http://openid.net/sreg/1.0')
						URI(CH.config.grails.openIdEndpointURL)
						'openid:Delegate'(CH.config.grails.serverURL + request.forwardURI)
					}
				}
			}
		}
	}
	
}